package com.baoxin.chenxixc4coach.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.telephony.PhoneNumberUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.GlideApp;
import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.bean.CourseStudent;
import com.baoxin.chenxixc4coach.utils.MyToast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

public class CourseStudentAdapter extends RecyclerView.Adapter<CourseStudentAdapter.ViewHolder> {
    private Context context;
    private List<CourseStudent> courseStudentList;

    public CourseStudentAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<CourseStudent> courseStudentList) {
        this.courseStudentList = courseStudentList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.course_student_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final CourseStudent student = courseStudentList.get(position);
        GlideApp.with(context)
                .load(student.getPhoto())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .error(R.drawable.default_)
                .into(holder.icon);
        holder.name.setText(student.getUserName());
        holder.phone.setText("手机号码：" + student.getPhone());
        holder.call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);//不是CALL不需要运行时权限
                Uri data = Uri.parse("tel:" + student.getPhone());
                intent.setData(data);
                context.startActivity(intent);
            }
        });

        holder.send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doSendSMSTo(student.getPhone(), "");
            }
        });
    }

    /**
     * @param phoneNumber
     * @param message
     */
    public void doSendSMSTo(String phoneNumber, String message) {
        if (PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)) {
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + phoneNumber));
            intent.putExtra("sms_body", message);
            context.startActivity(intent);
        }
    }


    @Override
    public int getItemCount() {
        return courseStudentList == null ? 0 : courseStudentList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView name;
        TextView phone;
        ImageView call;
        ImageView send;

        public ViewHolder(View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.icon);
            name = itemView.findViewById(R.id.name);
            phone = itemView.findViewById(R.id.phone);
            call = itemView.findViewById(R.id.call);
            send = itemView.findViewById(R.id.send_message);
        }
    }
}
